package unittests;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import javax.xml.parsers.ParserConfigurationException;
import static org.junit.Assert.*;
import org.junit.Test;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import spreadsheet.JDOMXMLReader;
import spreadsheet.SpreadSheet;
import spreadsheet.SpreadSheetCell;

public class JDOMXMLReaderTest {

	@Test
	public void test() {
		SpreadSheet spreadSheet = new SpreadSheet();
		spreadSheet.getCells()[0][0].setCellValue("=10");
		spreadSheet.getCells()[0][0].setCellType(SpreadSheetCell.CELL_TYPES_PRIMITIVE);
		spreadSheet.getCells()[0][1].setCellValue("=A1");
		spreadSheet.getCells()[0][1].setCellType(SpreadSheetCell.CELL_TYPES_FORMULA);
                spreadSheet.getCells()[0][2].setCellValue("=-9");
		spreadSheet.getCells()[0][2].setCellType(SpreadSheetCell.CELL_TYPES_PRIMITIVE);
                spreadSheet.getCells()[0][3].setCellValue("=-9.9");
		spreadSheet.getCells()[0][3].setCellType(SpreadSheetCell.CELL_TYPES_PRIMITIVE);
                spreadSheet.getCells()[0][4].setCellValue("=-A1");
		spreadSheet.getCells()[0][4].setCellType(SpreadSheetCell.CELL_TYPES_FORMULA);
                spreadSheet.getCells()[0][5].setCellValue("=-A1+2");
		spreadSheet.getCells()[0][5].setCellType(SpreadSheetCell.CELL_TYPES_FORMULA);
                spreadSheet.getCells()[0][6].setCellValue("=A1+A2");
		spreadSheet.getCells()[0][6].setCellType(SpreadSheetCell.CELL_TYPES_FORMULA);
                spreadSheet.getCells()[0][7].setCellValue("=(A1+2.0)");
		spreadSheet.getCells()[0][7].setCellType(SpreadSheetCell.CELL_TYPES_FORMULA);
		
		SpreadSheet spreadSheet2 = new SpreadSheet();
		try{
			spreadSheet.saveSpreadSheet("Test.xml");
			assertTrue(spreadSheet2.loadSpreadSheet("Test.xml")); // Test 1
		} catch (Exception e) {
			fail(e.getMessage());
		}
                assertEquals(spreadSheet2.getCells()[0][0].getCellValue(), spreadSheet.getCells()[0][0].getCellValue()); //Test 2
                assertEquals(spreadSheet2.getCells()[0][0].getCellType(), spreadSheet.getCells()[0][0].getCellType()); //Test 3
                assertEquals(spreadSheet2.getCells()[0][1].getCellValue(), spreadSheet.getCells()[0][1].getCellValue()); //Test 4
                assertEquals(spreadSheet2.getCells()[0][1].getCellType(), spreadSheet.getCells()[0][1].getCellType()); //Test 5
                assertEquals(spreadSheet2.getCells()[0][2].getCellValue(), spreadSheet.getCells()[0][2].getCellValue());
                assertEquals(spreadSheet2.getCells()[0][2].getCellType(), spreadSheet.getCells()[0][2].getCellType());
                assertEquals(spreadSheet2.getCells()[0][3].getCellValue(), spreadSheet.getCells()[0][3].getCellValue());
                assertEquals(spreadSheet2.getCells()[0][3].getCellType(), spreadSheet.getCells()[0][3].getCellType());
                assertEquals(spreadSheet2.getCells()[0][4].getCellValue(), spreadSheet.getCells()[0][4].getCellValue());
                assertEquals(spreadSheet2.getCells()[0][4].getCellType(), spreadSheet.getCells()[0][4].getCellType());
                assertEquals(spreadSheet2.getCells()[0][5].getCellValue(), spreadSheet.getCells()[0][5].getCellValue());
                assertEquals(spreadSheet2.getCells()[0][5].getCellType(), spreadSheet.getCells()[0][5].getCellType());
                assertEquals(spreadSheet2.getCells()[0][6].getCellValue(), spreadSheet.getCells()[0][6].getCellValue());
                assertEquals(spreadSheet2.getCells()[0][6].getCellType(), spreadSheet.getCells()[0][6].getCellType());
                assertEquals(spreadSheet2.getCells()[0][7].getCellValue(), spreadSheet.getCells()[0][7].getCellValue());
                assertEquals(spreadSheet2.getCells()[0][7].getCellType(), spreadSheet.getCells()[0][7].getCellType());

                SpreadSheet spreadSheet3 = new SpreadSheet();
                try {
                    assertFalse( spreadSheet3.loadSpreadSheet( "/path/to/nowhere.xml" ) );
                } catch( Exception e ) {
                    assertTrue( e instanceof FileNotFoundException );
                }
                assertFalse(spreadSheet3.loadSpreadSheet(""));
                assertFalse(spreadSheet3.loadSpreadSheet(null));
                
                SpreadSheetCell[][] spreadSheet4 = null;
                try {
                    spreadSheet4 = JDOMXMLReader.readXmlFile( "Test.xml" );
                } catch (ParserConfigurationException e) {
			fail(e.getMessage());
		} catch (SAXException e) {
			fail(e.getMessage());
		} catch (IOException e) {
			fail(e.getMessage());
		}
                assertEquals(spreadSheet4[0][0].getCellValue(), spreadSheet.getCells()[0][0].getCellValue());
                assertEquals(spreadSheet4[0][0].getCellType(), spreadSheet.getCells()[0][0].getCellType());
                assertEquals(spreadSheet4[0][1].getCellValue(), spreadSheet.getCells()[0][1].getCellValue());
                assertEquals(spreadSheet4[0][1].getCellType(), spreadSheet.getCells()[0][1].getCellType());
                assertEquals(spreadSheet4[0][2].getCellValue(), spreadSheet.getCells()[0][2].getCellValue());
                assertEquals(spreadSheet4[0][2].getCellType(), spreadSheet.getCells()[0][2].getCellType());
                assertEquals(spreadSheet4[0][3].getCellValue(), spreadSheet.getCells()[0][3].getCellValue());
                assertEquals(spreadSheet4[0][3].getCellType(), spreadSheet.getCells()[0][3].getCellType());
                assertEquals(spreadSheet4[0][4].getCellValue(), spreadSheet.getCells()[0][4].getCellValue());
                assertEquals(spreadSheet4[0][4].getCellType(), spreadSheet.getCells()[0][4].getCellType());
                assertEquals(spreadSheet4[0][5].getCellValue(), spreadSheet.getCells()[0][5].getCellValue());
                assertEquals(spreadSheet4[0][5].getCellType(), spreadSheet.getCells()[0][5].getCellType());
                assertEquals(spreadSheet4[0][6].getCellValue(), spreadSheet.getCells()[0][6].getCellValue());
                assertEquals(spreadSheet4[0][6].getCellType(), spreadSheet.getCells()[0][6].getCellType());
                assertEquals(spreadSheet4[0][7].getCellValue(), spreadSheet.getCells()[0][7].getCellValue());
                assertEquals(spreadSheet4[0][7].getCellType(), spreadSheet.getCells()[0][7].getCellType());
                new File( "Test.xml" ).delete();
                try {
                    spreadSheet4 = JDOMXMLReader.readXmlFile( "/path/to/nowhere.xml" );
                } catch( Exception e ) {
                    assertTrue( e instanceof FileNotFoundException );
                }
                assertNull( spreadSheet4 );
                try {
                    spreadSheet4 = JDOMXMLReader.readXmlFile( "" );
                } catch( Exception e ) {
                    assertTrue( e instanceof SAXParseException );
                }
                assertNull( spreadSheet4 );
                try {
                    spreadSheet4 = JDOMXMLReader.readXmlFile( null );
                } catch( Exception e ) {
                    assertTrue( e instanceof FileNotFoundException );
                }
                assertNull( spreadSheet4 );
	}
}